<template>
    <div>
        <div>
            <el-input v-model="jobLevel.name"
                      prefix-icon="el-icon-plus"
                      size="small"
                      class="jobLevelInput"
                      placeholder="添加职称...">
            </el-input>
            <el-select v-model="jobLevel.titleLevel" placeholder="职称等级" size="small" class="jobLevelSelect">
                <el-option
                        v-for="item in titleLevels"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-button @click="addJobLevel" size="small" type="primary" icon="el-icon-plus">添加</el-button>
        </div>
        <div>
            <el-table
                    class="jobListTab"
                    :data="jobLevels"
                    stripe
                    border
                    size="small"
                    style="width: 70%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="50">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职称名称"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="titleLevel"
                        label="职称级别">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success" size="small">已启用</el-tag>
                        <el-tag v-else type="danger" size="small">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="small"
                                @click="showJobLevelEditInfo(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="small"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button @click="deleteJobLevelByIds" size="small" type="danger" :disabled="this.multipleSelection.length==0" class="mulDelBtn">批量删除</el-button>
        </div>
        <!--编辑弹出框dialog-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="30%"
                >
            <div>
                <table>
                    <tr>
                        <td><el-tag size="small">职称名字</el-tag></td>
                        <td>
                            <el-input v-model="jobLevelEdit.name"
                                      prefix-icon="el-icon-plus"
                                      size="small"
                                      class="jobLevelEditInput"
                                      placeholder="修改的职称...">
                        </el-input>
                        </td>
                    </tr>
                    <tr>
                        <td><el-tag size="small">职称级别</el-tag></td>
                        <td>
                            <el-select v-model="jobLevelEdit.titleLevel" placeholder="修改的职称等级" size="small" class="jobLevelEditSelect">
                                <el-option
                                        v-for="item in titleLevels"
                                        :key="item"
                                        :label="item"
                                        :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td><el-tag type="small">是否启用</el-tag></td>
                        <td>
                            <el-switch
                                    v-model="jobLevelEdit.enabled"
                                    active-text="启用"
                                    inactive-text="禁用">
                            </el-switch>
                        </td>
                    </tr>
                </table>

            </div>
            <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
    <el-button size="small" type="primary" @click="doUpdateJobLevel">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "jobLevelManager",
        data(){
            return{
                jobLevel:{
                    name:'',
                    titleLevel:'',
                },
                titleLevels:[
                  '正高级',
                  '副高级',
                  '中级',
                  '初级',
                  '员级'
                ],
                jobLevels:[],
                dialogVisible:false,
                jobLevelEdit:{
                    name:'',
                    titleLevel:'',
                    enabled:null,
                },
                multipleSelection:[],

            }
        },
        mounted() {
            this.initJobLevels();
        },
        methods:{
            initJobLevels(){
                this.getRequest("/system/basic/jobLevel/").then(resp=>{
                    if(resp){
                        this.jobLevels=resp.data
                    }
                })
            },
            addJobLevel(){
                // 验空判断
                if(this.jobLevel.name && this.jobLevel.titleLevel){
                    this.postRequest("/system/basic/jobLevel/",this.jobLevel).then(resp=>{
                        if (resp){
                            this.initJobLevels();
                            // 将添加的信息的表单置为空
                            this.jobLevel={};
                        }
                    })
                }else {
                    this.$message.error("添加字段不能为空");
                }
            },

            showJobLevelEditInfo(index,data){
                this.dialogVisible=true;
                Object.assign(this.jobLevelEdit,data);
            },
            doUpdateJobLevel(){
                this.putRequest("/system/basic/jobLevel/",this.jobLevelEdit).then(resp=>{
                    if(resp){
                        this.initJobLevels();
                        this.dialogVisible=false;
                    }

                })
            },
            handleDelete(index,data){
                // 确认删除
                this.$confirm('此操作将永久删除['+data.name+']职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/jobLevel/"+data.id).then(resp=>{
                        if(resp){
                            this.initJobLevels();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                // val 选中的行的值 是数组
                this.multipleSelection = val;
            },
            deleteJobLevelByIds(){
                let ids='?';
                this.multipleSelection.forEach(item=>{
                    ids+="ids="+item.id+"&"
                });
                this.deleteRequest("/system/basic/jobLevel/"+ids).then(resp=>{
                    if(resp){
                        this.initJobLevels();
                    }
                })

            }
        }
    }
</script>

<style>
    .jobLevelInput{
        width: 300px;
    }
    .jobLevelEditInput{
        width: 180px;
    }
    .jobLevelEditSelect{
        width: 180px;
    }
    .jobLevelSelect{
        margin-left: 5px;
        margin-right: 5px;
    }
    .jobListTab{
        margin-top: 10px;
    }
    .mulDelBtn{
        margin-top: 8px;
    }
</style>